package com.javafactroy.kunlong.codefactroy.service.base;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;


@Data
@Accessors(chain = true)
@Slf4j
public class ToutFile {

    protected String outDbPath = System.getProperty("user.dir") + "/codegen/dbName";
    protected String outBasePath = outDbPath + "/tableName";

    protected String fileName;

    protected String prefix;
    protected String suffix;

    String dbName;
    String tableName;

    public ToutFile() {
        dbName = "db";
        tableName = "table";

        fileName = "Mapper.template.db.mapper";
        prefix = "";
        suffix = ".java";
    }

    public void rmDbFile() {
        String soutBasePath = StrUtil.replace(outDbPath, "dbName", dbName);
        FileUtil.del(soutBasePath);

    }

    public String getOutFileName(String clsName) {

        prefix = clsName;

        String subType = FileNameUtil.getSuffix(fileName);
        String fileType = FileNameUtil.getSuffix(FileNameUtil.getPrefix(fileName));

        String suf = suffix;
        String soutBasePath = StrUtil.replace(outBasePath, "dbName", dbName);
        soutBasePath = CharSequenceUtil.replace(soutBasePath, "tableName", tableName);

        if (fileName.startsWith("Mapper")) {

            suf = ".xml";
        }

        String sfileName = FileNameUtil.getPrefix(fileName);
        sfileName = FileNameUtil.getPrefix(sfileName);
        sfileName = FileNameUtil.getPrefix(sfileName);

        return soutBasePath + "/" + fileType + "/" + subType + "/" + prefix + sfileName + suf;

    }

}
